<?php

namespace app\api\controller\wm;


use app\common\controller\Api;
use app\common\model\wm\Order as ShopOrder;


/**
 * 外卖订单
 * @ApiWeigh (90)
 */
class Order extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['detail', 'payok'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    // protected $auth = ['test3'];

    /**
     * 订单结算页
     * @Author xy
     * @ApiTitle 
     * @ApiMethod (POST)
     * @ApiParams (name="goods_info", type="json", required=true, description="[{'id':1,'total':1},...]//商品ID 数量")
     * @ApiParams (name="admin_id", type="int", required=true, description="店铺ID")
     * @ApiParams (name="coupon_id", type="int", required=true, description="优惠劵id")
     * @ApiParams (name="address_id", type="int", required=false, description="地址ID")
     * @ApiReturnParams (name="code", type="integer", required=true, sample="0")
     * @ApiReturn ({'code':0,'msg':'返回成功'})
     */
    public function preorder()
    {
        $user = $this->auth->getUser();
        $goods_info = [];
        $data = $this->request->put();
        extract($data);
        $rej = $this->validate(
            $this->request->post(),
            [
                'admin_id' => 'require|number'
            ]
        );
        if (true !== $rej) {
            $this->responseMsg(1005, [], $rej);
        }
        $res = ShopOrder::getOrderPriceInfo($user, $admin_id, $goods_info, $address_id??0, $coupon_id??0);

        $this->responseObj($res);
    }

    /**
     * 提交订单
     * @Author xy
     * @ApiTitle 
     * @ApiMethod (POST)
     * @ApiParams (name="goods_info", type="json", required=true, description="[{'id':1,'total':1},...]//商品ID 数量")
     * @ApiParams (name="admin_id", type="int", required=true, description="店铺ID")
     * @ApiParams (name="coupon_id", type="int", required=true, description="优惠劵id")
     * @ApiParams (name="address_id", type="int", required=true, description="地址ID")
     * @ApiParams (name="remark", type="str", required=false, description="备注")
     * @ApiReturnParams (name="code", type="integer", required=true, sample="0")
     * @ApiReturn ({'code':0,'msg':'返回成功'})
     */
    public function addorder()
    {
        $user = $this->auth->getUser();
        $data = $this->request->put();
        extract($data);
        $rej = $this->validate(
            $data,
            [
                'goods_info'  => 'require|array',
                'admin_id' => 'require|number',
                'address_id'  => 'require|number',
            ]
        );
        if (true !== $rej) {
            $this->responseMsg(1005, [], $rej);
        }

        $res = ShopOrder::createOrder($user, $admin_id, $address_id, $goods_info,  $coupon_id, $remark);
        $this->responseObj($res);
    }





    /**
     * 我的订单列表
     * @Author xy
     * @ApiTitle 
     * @ApiMethod (POST)
     * @ApiParams (name="status", type="str", required=true, description="0=待付款,-1=已取消,1=配送中,99=已完成")
     * @ApiParams (name="page", type="int", required=false, description="页码")
     * @ApiReturnParams (name="code", type="integer", required=true, sample="0")
     * @ApiReturn ({'code':'0','msg':'返回成功'})
     */
    public function mylist()
    {
        $user = $this->auth->getUser();
        if ($this->request->post('status') === '') {
            $res = ShopOrder::with(['Ordergoods' => function ($query) {
                $query->withField('goods_info');
            }, 'admin' => function ($q) {
                $q->withField('nickname,avatar');
            }])->where('fa_wm_order.user_id', $user->id)->order('fa_wm_order.id desc')->paginate(10);
        } else {
            $status = strval($this->request->post('status'));
            if ($status == '2') {
                $res = ShopOrder::with(['Ordergoods' => function ($query) {
                    $query->withField('goods_info');
                }, 'admin' => function ($q) {
                    $q->withField('nickname,avatar');
                }])
                    ->where('fa_wm_order.user_id', $user->id)
                    ->where('fa_wm_order.status', 'in', ['1', '2', '3'])->order('fa_wm_order.id desc')->paginate(10);
            } else {
                $res = ShopOrder::with(['Ordergoods' => function ($query) {
                    $query->withField('goods_info');
                }, 'admin' => function ($q) {
                    $q->withField('nickname,avatar');
                }])
                    ->where('fa_wm_order.user_id', $user->id)
                    ->where('fa_wm_order.status', $status)->order('fa_wm_order.id desc')->paginate(10);
            }
        }

        // echo ShopOrder::getLastSql();
        // die;
        $this->responseMsg(0, $res);
    }





    /**
     * 订单详情
     * @Author xy
     * @ApiTitle 
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="integer", required=true, description="订单id")
     * @ApiReturnParams (name="code", type="integer", required=true, sample="0")
     * @ApiReturn ({'code':0,'msg':'返回成功'})
     */
    public function detail()
    {

        $rej = $this->validate(
            $this->request->post(),
            [
                'id'  => 'require|number',
            ]
        );

        if (true !== $rej) {
            $this->responseMsg(1005, [], $rej);
        }
        $res = ShopOrder::with(['Ordergoods' => function ($query) {
            $query->withField('goods_info');
        }, 'admin' => function ($q) {
            $q->withField('nickname,avatar');
        }])
            ->where('fa_wm_order.id', $this->request->post('id'))->find();
        if (!$res) {
            $this->responseMsg(1007, '', '没有找到数据');
        }

        $data = $res->toArray();
        // if ($res->status == '0') {
        //     $data['has_cancel_time'] = $res->createtime + 5 * 60 - time();
        // }
        // if ($res->status == '1') {
        //     $data['has_take_time'] = $res->pay_time + 15 * 60 - time();
        // }


        $this->responseMsg(0, $data);
    }


    /**
     * 取消订单
     * @Author xy
     * @ApiTitle 
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="integer", required=true, description="订单ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':0,'msg':'返回成功'})
     */
    public function cancel()
    {

        $rej = $this->validate(
            $this->request->post(),
            [
                'id'  => 'require|number',
            ]
        );

        if (true !== $rej) {
            $this->responseMsg(1005, [], $rej);
        }

        $user = $this->auth->getUser();

        $res = ShopOrder::get($this->request->post('id'));
        if (!$res) {
            $this->responseMsg(1007, '', '没有找到订单');
        }
        if ($res->user_id != $user->id) {
            $this->responseMsg(1006, '', '没有权限');
        }

        if ($res->status != '0') {
            $this->responseMsg(1006, '', '该订单不能取消');
        }

        $res->status = '-1';
        $res->save();
        $this->responseMsg(0);
    }

    /**
     * 确认完成订单
     * @Author xy
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="订单ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':0,'msg':'返回成功'})
     */
    public function confirm()
    {

        $rej = $this->validate(
            $this->request->post(),
            [
                'id'  => 'require|number',
            ]
        );

        if (true !== $rej) {
            $this->responseMsg(1005, [], $rej);
        }

        $user = $this->auth->getUser();

        $res = ShopOrder::get($this->request->post('id'));
        if (!$res) {
            $this->responseMsg(1007, '', '没有找到订单');
        }
        if ($res->user_id != $user->id) {
            $this->responseMsg(1006, '', '没有权限');
        }

        if ($res->status != '1') {
            $this->responseMsg(1006, '', '该订单不能完成');
        }

        $res->status = '99';
        $res->save();
        $this->responseMsg(0);
    }

    /**
     * 订单搜索
     * @Author xy
     * @ApiTitle 
     * @ApiMethod (POST)
     * @ApiParams (name="no", type="str", required=true, description="订单编号")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':0,'msg':'返回成功'})
     */
    public function search()
    {
        $rej = $this->validate(
            $this->request->post(),
            [
                'no'  => 'require|max:50',
            ]
        );

        if (true !== $rej) {
            $this->responseMsg(1005, [], $rej);
        }
        $res = ShopOrder::where('order_no', $this->request->post('no'))->whereOr('order_no', 'like', "%{$this->request->post('no')}")->find();
        if (!$res) {
            $this->responseMsg(1005, [], '没有查询到该订单');
        } else {
            $this->responseMsg(0, $res->id);
        }
    }



    /**
     * 测试支付完成
     * @Author xy
     * @ApiTitle 
     * @ApiMethod (POST)
     * @ApiSummary ()
     * @ApiBody ()
     * @ApiParams (name="id", type="integer", required=true, description="订单ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':0,'msg':'返回成功'})
     */
    public function payok()
    {

        $rej = $this->validate(
            $this->request->post(),
            [
                'id'  => 'require|number',
            ]
        );

        if (true !== $rej) {
            $this->responseMsg(1005, [], $rej);
        }
        $res = ShopOrder::payok($this->request->post('id'));

        $this->responseMsg(0);
    }
}
